# _*_ coding : utf-8 _*_
# @Time : 2023/3/14 0014 18:52
# @Author : 菜鸟王小二
# @File : 5_urllib_get请求的urlencode方法
# @Project : python爬虫

import urllib.request
import urllib.parse

base_url = 'https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&'

data = {
    'wd':'周杰伦',
    'sex':'男',
    'location':'中国台湾省'
}
# 将数据中多个中文转为unicode编码，并使用&拼接
new_data = urllib.parse.urlencode(data)
print(new_data)

url = base_url + new_data

headers = {
    'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.63'
}

request = urllib.request.Request(url=url,headers=headers)

response = urllib.request.urlopen(request)

text = response.read().decode('utf-8')

print(text)